<?php ob_start(); 
session_start(); // on relaye la session 
if(isset($_SESSION["is_registered"]) && $_SESSION["is_registered"] == "registered"){
		$enregistrer = $_SESSION["is_registered"];
}else{
	header("location:../../../menu.php"); // redirection en cas d'echec
}
require_once('../../../Connections/microassur.php'); 
require_once('../../../utils/fonctions.php'); 
require_once('../inclure.php');
$message = ''; 
?>
<?php
if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") 
{
  $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;

  $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);

  switch ($theType) {
    case "text":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;    
    case "long":
    case "int":
      $theValue = ($theValue != "") ? intval($theValue) : "NULL";
      break;
    case "double":
      $theValue = ($theValue != "") ? "'" . doubleval($theValue) . "'" : "NULL";
      break;
    case "date":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;
    case "defined":
      $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
      break;
  }
  return $theValue;
}
}

$editFormAction = $_SERVER['PHP_SELF'];
if (isset($_SERVER['QUERY_STRING'])) {
  $editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']);
}

$colname_rs_mod_dico = "-1";
if (isset($_GET['Identifiant_table'])) {
  $colname_rs_mod_dico = $_GET['Identifiant_table'];
}
mysql_select_db($database_microassur, $microassur);
$query_rs_mod_dico = sprintf("SELECT * FROM dictionnaire WHERE Identifiant_table = %s", GetSQLValueString($colname_rs_mod_dico, "int"));
$rs_mod_dico = mysql_query($query_rs_mod_dico, $microassur) or die(mysql_error());
$row_rs_mod_dico = mysql_fetch_assoc($rs_mod_dico);
$totalRows_rs_mod_dico = mysql_num_rows($rs_mod_dico);

if(isset($_POST['Code_table']) && isset($row_rs_mod_dico['Code_table'])){
	$comparer = strcmp($_POST['Code_table'], $row_rs_mod_dico['Code_table']);
}

if(isset($_POST['Type_table']) && $row_rs_mod_dico['Type_table']){
	$comparer1 = strcmp($_POST['Type_table'], $row_rs_mod_dico['Type_table']);
}

if(isset($_POST['Code_table']) && empty($_POST['Code_table'])){
	$message = colorer("Vous devez saisir le CODE.","false");
}elseif(isset($_POST['Code_table']) && !veriflibelle1('Code_table') && caracteres_interdits($_POST['Code_table'])==1){
	$message = colorer("Le code saisi comporte des caractères spéciaux interdits.","false");
}elseif ( isset($_POST['Code_table']) && isset($row_rs_mod_dico['Code_table']) && isset($comparer) && $comparer ){
	$message = colorer("Vous ne pouvez pas modifier le code de la table","false");
} // Fin if($comparer)
elseif(isset($_POST['Libelle_table']) && empty($_POST['Libelle_table'])){
	$message = colorer("Vous devez saisir le LIBELLE.","false");
}elseif(isset($_POST['Libelle_table']) && caracteres_interdits($_POST['Libelle_table'])==1){
	$message = colorer("Le libellé saisi comporte des caractères spéciaux interdits.","false");
}elseif ( isset($_POST['Type_table']) && isset($row_rs_mod_dico['Type_table']) && isset($comparer1) && $comparer1 ){
	$message = colorer("Vous ne pouvez pas modifier le type de la table","false");
}
else{
	if ((isset($_POST["MM_update"])) && ($_POST["MM_update"] == "form1")) {
	  $updateSQL = sprintf("UPDATE dictionnaire SET Libelle_table=%s WHERE Identifiant_table=%s",
						   //GetSQLValueString($_POST['Code_table'], "text"),
						   GetSQLValueString($_POST['Libelle_table'], "text"),
						   //GetSQLValueString($_POST['Type_table'], "text"),
						   GetSQLValueString($_POST['Identifiant_table'], "int"));
	
		mysql_select_db($database_microassur, $microassur);
	  //mysql_query("START TRANSACTION");
	  $Result1 = mysql_query($updateSQL, $microassur) or die(mysql_error());
	  if($Result1){
	  		//mysql_query("commmit");
	  		$message = colorer("La modification du dictionnaire est prise en compte","true");
			if($_POST['Libelle_table']!=$row_rs_mod_dico['Libelle_table'])
				$row_rs_mod_dico['Libelle_table'] = $_POST['Libelle_table'];
		}
	  else{
	  		//mysql_query("rollback");
			$message = colorer("Contacter la DSI, erreur modification dictionnaire","false");
		}
	}	

} // Fin else $comparer

?>
<?php 
	if( isset($message) && !empty($message) ){
		//echo $message;
	}elseif(isset($_GET['msg']) && !empty($_GET['msg'])){
		$message = $_GET['msg'];
		switch($message){
			case "rattach" :
				$message = colorer("Vous ne pouvez supprimer ce dictionnaire, au moins un élément lui est rattaché","false");
				break;
			case "supp" :
				$message = colorer("Le dictionnaire est supprimé","true");
				break;
			case "err_supp" :
				$message = colorer("Contacter la DSI, erreur suppression dictionnaire","false");
				break;
			case "supp" :
				$message = colorer( "Le dictionnaire est supprimé","false");
				break;
			default :
				$message = colorer("Contacter la DSI, erreur suppression dictionnaire","false");
		}
		//echo $message;
	}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
		<meta http-equiv="X-UA-Compatible" content="IE=8" />
			<title><?php echo $titleGen ?></title>
			<link rel="stylesheet" type="text/css" href="../../../css/main.css" />
            <link rel="stylesheet" type="text/css" href="list.css" />
			<!--[if IE 7]>
            	<link rel="stylesheet" type="text/css" href="../../../css/ie7.css" />
            	<link rel="stylesheet" type="text/css" href="form_ie7.css" />
            <![endif]-->
	</head>
<body>
			<div id="sf_menu1">
				<h3>Administration</h3>
				<?php
					if(isset($message))
						echo $message;
					$nommodule="Dictionnaire";
					$nommodule2="Détail dictionnaire";
					?>
			</div>
			<div class="cleared"></div>
			<!-- /*******************************/ -->
			<div class="corps1">
<form action="<?php echo $editFormAction; ?>" method="post" name="form1" id="form1">
			<div class="imgleft"></div>
			<div class="imgmiddle">
				<div class="btn"><span class="btnleft"></span><span class="btnmiddle"><h5><?php echo $nommodule; ?></h5></span><span class="btnright"></span></div>
				<div class="btn1"><span class="btnleft"></span><span class="btnmiddle"><h5><?php echo $nommodule2; ?></h5></span><span class="btnright"></span></div>
					<div class="formulaire">
					<table class="tabform">
						<tr>
						  <td>CODE</td>
						  <td><input class="resize_1" type="text" name="Code_table" maxlength="5" value="<?php if(isset($_POST['Code_table'])){ echo remind_text('Code_table'); }else{ echo htmlentities($row_rs_mod_dico['Code_table'], ENT_COMPAT, 'utf-8');} ?>" size="32" /></td>
						</tr>
						<tr>
						  <td>LIBELLE</td>
						  <td><input type="text" name="Libelle_table"  maxlength="30" value="<?php if(isset($_POST['Libelle_table'])){ echo remind_text('Libelle_table'); }else{ echo htmlentities($row_rs_mod_dico['Libelle_table'], ENT_COMPAT, 'utf-8');} ?>" size="32" /></td>
						</tr>
						<tr>
						  <td>TYPE</td>
						  <td><select name="Type_table">
							<option value="T" <?php if(isset($_POST['Type_table'])){if (!(strcmp("T", htmlentities($_POST['Type_table'], ENT_COMPAT, 'utf-8')))) {echo "SELECTED";}}else{ if (!(strcmp("T", htmlentities($row_rs_mod_dico['Type_table'], ENT_COMPAT, 'utf-8')))) {echo "SELECTED";} } ?>>Technique</option>
							<option value="M" <?php if(isset($_POST['Type_table'])){if (!(strcmp("M", htmlentities($_POST['Type_table'], ENT_COMPAT, 'utf-8')))) {echo "SELECTED";}}else{ if (!(strcmp("M", htmlentities($row_rs_mod_dico['Type_table'], ENT_COMPAT, 'utf-8')))) {echo "SELECTED";} } ?>>Métier</option>
						  </select>
						  </td>
						</tr>
					</table>
				</div>
			</div>
			<div class="imgright"></div>				
				<div class="cleared"></div>
				
					<div class="bt1"><a href="./index.php"><span class="btleft"></span><span class="btmiddle">Retour</span><span class="btright"></span></a></div>
					<div class="bt"><a <?php if(isset($row_rs_mod_dico['Identifiant_table'])){ ?> href="suppdictionnaire.php?Identifiant_table=<?php echo $row_rs_mod_dico['Identifiant_table']; ?>" <?php } else echo 'href="#"';?>><span class="btleft"></span><span class="btmiddle">Supprimer</span><span class="btright"></span></a></div>
					<div class="bt"><span class="btleft"></span><input <?php if(retourValForm('Identifiant_table')==NULL || retourValForm('msg')=='supp') echo "disabled='disabled'"; ?> type="submit" name="Modifier" value="Modifier" class="resizeall btmiddle"/><span class="btright"></span></div>
					
				<div class="cleared"></div>
  <input type="hidden" name="Identifiant_table" value="<?php echo $row_rs_mod_dico['Identifiant_table']; ?>" />
  <input type="hidden" name="MM_update" value="form1" />
</form>
</div>
</body>
</html>
<?php
mysql_free_result($rs_mod_dico);
?>
<?php ob_end_flush();?>